{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/gemini.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Google GenAI Embeddings\n",
    "\n",
    "Using Google's `google-genai` package, LlamaIndex provides a `GoogleGenAIEmbedding` class that allows you to embed text using Google's GenAI models from both the Gemini and Vertex AI APIs."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install llama-index-embeddings-google-genai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"GOOGLE_API_KEY\"] = \"...\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "`GoogleGenAIEmbedding` is a wrapper around the `google-genai` package, which means it supports both Gemini and Vertex AI APIs out of that box.\n",
    "\n",
    "You can pass in the `api_key` directly, or pass in a `vertexai_config` to use the Vertex AI API.\n",
    "\n",
    "Other options include `embed_batch_size`, `model_name`, and `embedding_config`.\n",
    "\n",
    "The default model is `text-embedding-004`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.embeddings.google_genai import GoogleGenAIEmbedding\n",
    "from google.genai.types import EmbedContentConfig\n",
    "\n",
    "embed_model = GoogleGenAIEmbedding(\n",
    "    model_name=\"text-embedding-004\",\n",
    "    embed_batch_size=100,\n",
    "    # can pass in the api key directly\n",
    "    # api_key=\"...\",\n",
    "    # or pass in a vertexai_config\n",
    "    # vertexai_config={\n",
    "    #     \"project\": \"...\",\n",
    "    #     \"location\": \"...\",\n",
    "    # }\n",
    "    # can also pass in an embedding_config\n",
    "    # embedding_config=EmbedContentConfig(...)\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Usage"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Sync"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.031099992, 0.02192731, -0.06523498, 0.016788177, 0.0392835]\n",
      "Dimension of embeddings: 768\n"
     ]
    }
   ],
   "source": [
    "embeddings = embed_model.get_text_embedding(\"Google Gemini Embeddings.\")\n",
    "print(embeddings[:5])\n",
    "print(f\"Dimension of embeddings: {len(embeddings)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.022199392, 0.03671178, -0.06874573, 0.02195774, 0.05475164]\n",
      "Dimension of embeddings: 768\n"
     ]
    }
   ],
   "source": [
    "embeddings = embed_model.get_query_embedding(\"Query Google Gemini Embeddings.\")\n",
    "print(embeddings[:5])\n",
    "print(f\"Dimension of embeddings: {len(embeddings)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got 3 embeddings\n",
      "Dimension of embeddings: 768\n"
     ]
    }
   ],
   "source": [
    "embeddings = embed_model.get_text_embedding_batch(\n",
    "    [\n",
    "        \"Google Gemini Embeddings.\",\n",
    "        \"Google is awesome.\",\n",
    "        \"Llamaindex is awesome.\",\n",
    "    ]\n",
    ")\n",
    "print(f\"Got {len(embeddings)} embeddings\")\n",
    "print(f\"Dimension of embeddings: {len(embeddings[0])}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Async"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.031099992, 0.02192731, -0.06523498, 0.016788177, 0.0392835]\n",
      "Dimension of embeddings: 768\n"
     ]
    }
   ],
   "source": [
    "embeddings = await embed_model.aget_text_embedding(\"Google Gemini Embeddings.\")\n",
    "print(embeddings[:5])\n",
    "print(f\"Dimension of embeddings: {len(embeddings)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.022199392, 0.03671178, -0.06874573, 0.02195774, 0.05475164]\n",
      "Dimension of embeddings: 768\n"
     ]
    }
   ],
   "source": [
    "embeddings = await embed_model.aget_query_embedding(\n",
    "    \"Query Google Gemini Embeddings.\"\n",
    ")\n",
    "print(embeddings[:5])\n",
    "print(f\"Dimension of embeddings: {len(embeddings)}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Got 3 embeddings\n",
      "Dimension of embeddings: 768\n"
     ]
    }
   ],
   "source": [
    "embeddings = await embed_model.aget_text_embedding_batch(\n",
    "    [\n",
    "        \"Google Gemini Embeddings.\",\n",
    "        \"Google is awesome.\",\n",
    "        \"Llamaindex is awesome.\",\n",
    "    ]\n",
    ")\n",
    "print(f\"Got {len(embeddings)} embeddings\")\n",
    "print(f\"Dimension of embeddings: {len(embeddings[0])}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  },
  "vscode": {
   "interpreter": {
    "hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
